package xyz.myachin.downloader.ui

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import xyz.myachin.downloader.ACTION_REMOVE_ALL
import xyz.myachin.downloader.R
import xyz.myachin.downloader.dm.Downloader
import xyz.myachin.downloader.settings.Settings
import xyz.myachin.downloader.ui.share.SharingActivity

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        private var mCleanNow: Preference? = null
        private var mDisableIcon: Preference? = null
        private var mProxyFeature: SwitchPreferenceCompat? = null

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

            setCleanNowAction()
            setDisableIconAction()
            setProxyFeature()
        }

        private fun setProxyFeature() {
            mProxyFeature = findPreference("key_file_redirection")
            findPreference<SwitchPreferenceCompat>("key_file_redirection")?.summary =
                getString(R.string.app_will_accepts_urls, getString(R.string.app_name))
            mProxyFeature?.setOnPreferenceChangeListener { _, newValue ->
                when (newValue as Boolean) {
                    true -> {
                        Settings.isProxyMustDisabled = false
                        Settings.isProxyEnabled = true
                        Settings.setProxyFeatureState(
                            context?.applicationContext!!,
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                        )
                    }
                    false -> {
                        Settings.isProxyMustDisabled = true
                        Settings.isProxyEnabled = false
                        Settings.setProxyFeatureState(
                            context?.applicationContext!!,
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                        )
                    }
                }
                true
            }
        }

        private fun setDisableIconAction() {
            if (Settings.hideLauncherIcon) return

            mDisableIcon = findPreference("key_disable_icon")
            mDisableIcon?.onPreferenceClickListener =
                Preference.OnPreferenceClickListener {
                    Intent(context?.applicationContext!!, DisableIconActivity::class.java).run {
                        startActivity(this)
                    }
                    true
                }

        }

        private fun setCleanNowAction() {
            mCleanNow = findPreference("key_clean_now")

            Thread {
                if (Downloader.getFinishedDownloads() != null && Downloader.getFinishedDownloads()!!.count > 0) {
                    mCleanNow?.isVisible = true
                    mCleanNow?.onPreferenceClickListener =
                        Preference.OnPreferenceClickListener {
                            Intent(ACTION_REMOVE_ALL).run {
                                setClass(context?.applicationContext!!, SharingActivity::class.java)
                                startActivity(this)
                                mCleanNow?.isVisible = false
                            }
                            true
                        }
                }
            }.start()
        }
    }
}